/**
 * 二分查找
 * 条件：数组已经排好序
 */

public class ArraySearch {
    public static void main(String[] args) {
        int[] arr = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8};
        int dest = 8;
        int begin = 0;
        int end = arr.length - 1;
        int mid = 0;
        boolean isFind = false;
        while (end - begin >= 0) {
            mid = (end + begin) / 2;
            if (arr[mid] == dest) {
                isFind = true;
                break;
            } else if (arr[mid] > dest) {
                end = mid - 1;
            } else {
                begin = mid + 1;
            }
        }
        if (isFind) {
            System.out.println("目标在：" + mid);
        } else {
            System.out.println("未找到目标");
        }
    }


}
